//
//  HLPhotosWallView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/13.
//

#import "HLPhotosWallView.h"
#import "HLPickerManager.h"
#import "HLUserDetailViewController.h"

@interface HLPhotosWallView ()
@property (nonatomic, strong) UILabel *tagsLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) QMUIButton *arrowButton;
@property (nonatomic, strong) QMUIGridView *imageContentView;
@end

@implementation HLPhotosWallView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        
    }
    return self;
}

- (void)setupViews
{
    self.backgroundColor = UIColorWhite;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 8;
    
    self.closeButton = [[QMUIButton alloc] init];
    [_closeButton setImage:UIImageMake(@"im_sessionHeader_close") forState:UIControlStateNormal];
    [self addSubview:_closeButton];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(4);
        make.right.mas_equalTo(-4);
    }];
    
    self.tagsLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:HLTextColor1];
    _tagsLabel.text = @"标签：女神，活力，才艺，温柔，可爱";
    _tagsLabel.numberOfLines = 0;
    [self addSubview:_tagsLabel];
    [_tagsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(12);
        make.right.mas_equalTo(-12);
    }];
    
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = HLLineColor;
    [self addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_tagsLabel);
        make.top.equalTo(_tagsLabel.mas_bottom).offset(8);
        make.height.mas_equalTo(0.5);
    }];
    
    self.titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(13) textColor:HLTextColor1];
    _titleLabel.text = @"照片墙";
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_tagsLabel);
        make.top.equalTo(_lineView.mas_bottom).offset(8);
    }];
    
    self.arrowButton = [[QMUIButton alloc] init];
    [_arrowButton setImage:UIImageMake(@"im_sessionHeader_arrow") forState:UIControlStateNormal];
    [_arrowButton addTarget:self action:@selector(moreEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_arrowButton];
    [_arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLabel);
        make.right.mas_equalTo(-6);
    }];
    
    CGFloat width = (SCREEN_WIDTH - 64) / 5;
    self.imageContentView = [[QMUIGridView alloc] init];
    _imageContentView.columnCount = 5;
    _imageContentView.rowHeight = width;
    _imageContentView.separatorWidth = 4;
    _imageContentView.separatorColor = UIColorWhite;
    _imageContentView.separatorDashed = NO;
    [self addSubview:_imageContentView];
    [_imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_lineView);
        make.top.equalTo(_titleLabel.mas_bottom).offset(8);
        make.height.mas_equalTo(width + 4);
        make.bottom.mas_equalTo(-16);
    }];
    
}

- (void)setUserInfor:(HLUserInfoModel *)userInfor
{
    _userInfor = userInfor;
    
    NSString *tags = [[userInfor.tagVOS qmui_mapWithBlock:^NSString * _Nonnull(HLHomeTagModel * _Nonnull item, NSInteger index) {
        return item.tagName;
    }] componentsJoinedByString:@", "];
    self.tagsLabel.text = [NSString stringWithFormat:@"标签：%@", tags.length > 0 ? tags : @"暂无标签"];
    [_imageContentView qmui_removeAllSubviews];
    NSArray *opuses = [userInfor imagesWithUserOpuses];
    NSInteger count = opuses.count > 5 ? 5 : opuses.count;
    
    for (int i = 0; i < count; i++) {
        HLOpusModel *photo = opuses[i];
        QMUIButton *previewButton = [[QMUIButton alloc] init];
        previewButton.imageView.image = [HLUIHelper avatarPlaceholder];
        previewButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
        previewButton.tag = HLCommonTagA + i;
        previewButton.cornerRadius = 8;
        [previewButton addTarget:self action:@selector(previewAction:) forControlEvents:UIControlEventTouchUpInside];
        [previewButton sd_setImageWithURL:[NSURL URLWithString:photo.opusLink] forState:UIControlStateNormal placeholderImage:[HLUIHelper avatarBigPlaceholder]];
        [_imageContentView addSubview:previewButton];
    }
}

- (void)moreEvent:(QMUIButton *)btn
{
    HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:_userInfor.userId];
    [self.qmui_viewController.navigationController pushViewController:userDetailVC animated:YES];
}

- (void)previewAction:(QMUIButton *)btn
{
    NSInteger index = btn.tag - HLCommonTagA;
    
    HXPhotoManager *photoManager = [HXPhotoManager managerWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    NSArray *opuses = [_userInfor imagesWithUserOpuses];
    NSMutableArray *models = [NSMutableArray arrayWithCapacity:opuses.count];
    for (HLOpusModel *opus in opuses) {
        HXCustomAssetModel *model = [HXCustomAssetModel assetWithNetworkImageURL:[NSURL URLWithString:opus.opusLink] selected:YES];
        [models addObject:model];
    }
    [photoManager addCustomAssetModel:models];
    [HLPickerManager previewWithManager:photoManager index:index controller:self.qmui_viewController];
}

- (void)closeAction
{
    
    NSString *photoWallKey =  [NSString stringWithFormat:@"PhotosWall_%@_%@", [HLRunTime shareInstance].mine.userId, _userInfor.userId];
    NSDate *now = [NSDate date];
    [[NSUserDefaults standardUserDefaults] setValue:now forKey:photoWallKey];
}


+ (BOOL)isHideWithUserId:(NSString *)otherUserId
{
   NSString *photoWallKey =  [NSString stringWithFormat:@"PhotosWall_%@_%@", [HLRunTime shareInstance].mine.userId, otherUserId];
    NSDate *currentDate = [[NSUserDefaults standardUserDefaults] objectForKey:photoWallKey];
    if ([currentDate isKindOfClass:NSDate.class]) {
        return [currentDate br_day];
    }
    
    return false;
}

@end
